﻿///////////////////////////////////////////////////////////////////////////////
//	Copyright 2013 JASDev International
//
//	Licensed under the Apache License, Version 2.0 (the "License");
//	you may not use this file except in compliance with the License.
//	You may obtain a copy of the License at
//
//		http://www.apache.org/licenses/LICENSE-2.0
//
//	Unless required by applicable law or agreed to in writing, software
//	distributed under the License is distributed on an "AS IS" BASIS,
//	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//	See the License for the specific language governing permissions and
//	limitations under the License.
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.Text;

namespace JDI.Pusher.Client
{
	/// <summary>
	/// Represents a collection of PusherMembers.
	/// </summary>
	public class PusherMembers
	{
		#region Constructors and IDisposable

		internal PusherMembers()
		{
			this.isDisposed = false;
			this.members = new Hashtable();
		}

		~PusherMembers()
		{
			this.Dispose(false);
		}

		internal void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (this.isDisposed)
				return;

			if (disposing)
			{
				// cleanup
				if (this.members != null)
					this.members.Clear();
			}

			// set everything to null
			this.members = null;

			// make sure we don't dispose again
			this.isDisposed = true;
		}

		#endregion


		#region Properties

		public PusherMember this[string userId]
		{
			get
			{
				PusherMember member = null;
				lock (this.members.SyncRoot)
				{
					if (this.members.Contains(userId))
					{
						member = new PusherMember(userId, this.members[userId]);
					}
				}
				return member;
			}
		}

		public PusherMember Me
		{
			// TODO - implement PusherMembers::Me
			get
			{
				PusherMember member = null;
				lock (this.members.SyncRoot)
				{
					if (this.members.Contains(""))
					{
						member = (PusherMember)this.members[""];
					}
				}
				return member;
			}
		}

		public int Count
		{
			get
			{
				int count = 0;
				lock (this.members.SyncRoot)
				{
					count = this.members.Count;
				}
				return Count;
			}
		}

		#endregion


		#region Methods

		internal void Add(PusherMessage.MemberData member)
		{
			lock (this.members.SyncRoot)
			{
				this.members[member.UserId] = member.UserInfo;
			}
		}

		internal void Remove(string userId)
		{
			lock (this.members.SyncRoot)
			{
				if (this.members.Contains(userId))
				{
					this.members.Remove(userId);
				}
			}
		}

		internal void Clear()
		{
			lock (this.members.SyncRoot)
			{
				this.members.Clear();
			}
		}

		internal bool Contains(string userId)
		{
			lock (this.members.SyncRoot)
			{
				return this.members.Contains(userId);
			}
		}

		#endregion


		#region Member Fields

		protected bool isDisposed;
		protected Hashtable members;

		#endregion
	}
}
